<?php


namespace app\api\model;


use app\common\library\IDMan;
use app\common\model\BaseModel;
use common;
use ApiOper;
use think\Db;

class BikeDispatchModel extends BaseModel
{
    protected $name = 'dispatch_order';
    protected $pk = 'id';

    //派单
    function add($info){
        $paramlist = [
            'sn' =>'string',
            'city_id' => 'string',
            'bike_id' => 'string',
            'repark_index' =>'int',
            'staff_id' => 'string',
            'staff_name' => 'string',
            'staff_phone' => 'string',
            'dispatch_type' => 'int',
            'remark' => 'string',
        ];

        $data = common::type_conver($info, $paramlist);

        if (!$data) {
           $this->throw_myexception(X_ERROR, "派单列表错误!");
        }

        $data['id']  = IDMan::city_order_id($data['city_id']);
        $data['state'] = CS_DISPATCH_ORDER_STATE_SUBMIT;

        ApiOper::instance()->append_creater_info($data);


        self::create($data);


        //设置派单
        $data2 =[
            'is_dispatched' => 1,
            'last_dispatch_order_id' =>$data['id'],
        ];

        $where2 =[
            'id' => $info['bike_id'],
            'is_deleted'=>0,
        ];
        Db::table('tb_bike')->where($where2)->update($data2);
//        hisql::update('tb_bike', $data2, $where2);

        return $data;
    }

    //撤销
    function del($bike_id){
        $data = [
            'state' => CS_DISPATCH_ORDER_STATE_CANCEL,
        ];
        $where = [
            'bike_id' => $bike_id,
            'state' => 0,
        ];

        self::update($data, $where);

        $data2 =[
            'is_dispatched' => 0
        ];

        $where2 =[
            'id' => $bike_id,
            'is_deleted'=>0,
        ];
        Db::table('tb_bike')->where($where2)->update($data2);

        return 1;
    }

    function del_by_sn($bike_sn){
        $data = [
            'state' => CS_DISPATCH_ORDER_STATE_CANCEL,
        ];
        $where = [
            'sn' => $bike_sn,
            'state' => 0,
        ];

        self::update($data, $where);

        $data2 =[
            'is_dispatched' => 0
        ];

        $where2 =[
            'sn' => $bike_sn,
            'is_deleted'=>0,
        ];
        Db::table('tb_bike')->where($where2)->update($data2);

        return 1;
    }

    //查询
    function query_by_id($bike_id){
        $paramlist = [
            'id' =>'string',
            'sn' =>'string',
            'city_id' => 'string',
            'bike_id' => 'string',
            'repark_index' =>'int',
            'creator_id' =>'string',
            'creator_name'=>'string',
            'create_time' => 'string',
            'staff_id' => 'string',
            'staff_name' =>'string',
            'end_time' =>'string',
            'dispatch_type' => 'int',
            'remark' => 'string',
            'is_deleted' => 'int',
            'com_staff_id' => 'string',
            'com_staff_name' =>'string',
            'com_staff_type' => 'int',
            'state' => 'int',
        ];

        $sfs = common::array_keys($paramlist);
        //$st = "select SQL_CALC_FOUND_ROWS $sfs  from tb_dispatch_order where  bike_id = ? and state = 0 and is_deleted = 0";

        $beans = [
            'bike_id'=>$bike_id,
            'state' => 0,
            'is_deleted' => 0
        ];


        return $this->where($beans)->field($sfs)->find();
    }

    function query_by_sn($bike_sn){
        $paramlist = [
            'id' =>'string',
            'sn' =>'string',
            'city_id' => 'string',
            'bike_id' => 'string',
            'repark_index' =>'int',
            'creator_id' =>'string',
            'creator_name'=>'string',
            'create_time' => 'string',
            'staff_id' => 'string',
            'staff_name' =>'string',
            'end_time' =>'string',
            'dispatch_type' => 'int',
            'remark' => 'string',
            'is_deleted' => 'int',
            'com_staff_id' => 'string',
            'com_staff_name' =>'string',
            'com_staff_type' => 'int',
            'state' => 'int',
        ];

        $sfs = common::array_keys($paramlist);
        $beans = [
            'sn'=>$bike_sn,
            'state' => 0,
            'is_deleted' => 0
        ];


        return $this->where($beans)->field($sfs)->find();
    }


    //查询info
    function query_info($id){
        $paramlist = [
            'id' =>'string',
            'sn' =>'string',
            'city_id' => 'string',
            'bike_id' => 'string',
            'repark_index' =>'int',
            'creator_id' =>'string',
            'creator_name'=>'string',
            'create_time' => 'string',
            'staff_id' => 'string',
            'staff_name' =>'string',
            'end_time' =>'string',
            'dispatch_type' => 'int',
            'remark' => 'string',
            'is_deleted' => 'int',
            'com_staff_id' => 'string',
            'com_staff_name' =>'string',
            'com_staff_type' => 'int',
            'state' => 'int',
        ];

        $sfs = common::array_keys($paramlist);

        $beans = [
            'id'=>$id,
        ];

        return $this->where($beans)->field($sfs)->find();
    }

    //更新数据
    function complete_stask($sn,$info){

        $data =[
            'state' => CS_DISPATCH_ORDER_STATE_FINISHED,
            'end_time' =>date('Y-m-d H:i:s'),
            'com_staff_type' => $info['com_staff_type'],
            'com_staff_id' => $info['com_staff_id'],
            'com_staff_name' => $info['com_staff_name']
        ];

        $where = [
            'sn' => $sn,
            'state' =>0,
        ];

        self::update($data, $where);


        //设置派单
        $data2 =[
            'is_dispatched' => 0
        ];

        $where2 =[
            'sn' => $sn,
            'is_deleted'=>0,
        ];
        //hisql::update('tb_bike', $data2, $where2);
        Db::table('tb_bike')->where($where2)->update($data2);
    }

    function query_list($city_id,$cd){
        $limit = $cd['limit'];

        $paramlist = [
            'id' =>'string',            //单号
            'sn' =>'string',            //车辆sn
            'city_id' => 'string',      //城市id
            'bike_id' => 'string',      //车辆id
            'repark_index' =>'int',     //差几
            'creator_id' =>'string',    //派单者id
            'creator_name'=>'string',   //派单着名字
            'create_time' => 'string',  //派单时间
            'staff_id' => 'string',     //被派单着id
            'staff_name' =>'string',    //被派单着名字
            'staff_phone' => 'string',  //被派单的手机号码
            'end_time' =>'string',      //完成时间
            'dispatch_type' => 'int',   //0:挪车
            'remark' => 'string',       //缘由
            'is_deleted' => 'int',      //是否取消
            'com_staff_id' => 'string',     //真实执行者id
            'com_staff_name' =>'string',    //真实执行者名字
            'com_staff_type' => 'int',      //0：运维，1:玩家
            'state' => 'int',               //0:派单，100完成 101撤销
        ];



        $sfs = common::array_keys($paramlist);

       // $st = "select SQL_CALC_FOUND_ROWS $sfs  from tb_dispatch_order where is_deleted = 0";

        $beans = [
            'is_deleted'=>0
        ];

        if (isset($city_id)) {
            //$st = $st . " and city_id = ? ";
            $beans['city_id'] = $city_id;
        }


        if (isset($cd['start_time'])) {
            $beans['create_time'] = ['>=',$cd['start_time']];
        }
        if (isset($cd['end_time'])) {
            $beans['create_time'] = ['<=',$cd['end_time']];
        }


        if (isset($cd['staff_id'])) {
            $beans['staff_id'] = $cd['staff_id'];
        }

        if (isset($cd['state'])) {
            $beans['state'] = $cd['state'];
        }

        if (isset($cd['not_state'])) {
            $beans['state'] = ['<>',$cd['not_state']];
        }


        if (isset($cd['sk']) && !empty($cd['sk'])) {
            $sk = $cd['sk'];
            if (IDMan::is_bike_sn($sk)) {
                $beans['sn'] = $sk;
            } else if (IDMan::is_bike_id($sk)) {
                $beans['bike_id'] = $sk;
            }
        }


        $rows = $this->where($beans)->field($sfs)->limit($cd['pos'],$limit)->select();

        return $rows->toArray();
    }



    function query_dispatch_count($city_id,$staff_id)
    {
        return Db::table('tb_dispatch_order')->where(['city_id' => $city_id, 'is_deleted' => 0,'state' =>0,'staff_id'=>$staff_id])->count('id');
        //return hisql::count('tb_dispatch_order', ['city_id' => $city_id, 'is_deleted' => 0,'state' =>0,'staff_id'=>$staff_id], null);
    }

    //分析日志
    function analyz_month_date($city_id,$condition){
        $condition['not_state'] = 101;
        $condition['staff_id'] = ApiOper::instance()->userinfo()['id'];

        $list = $this->query_list($city_id,$condition);

        $vaild = 0;
        $not_vaild = 0;
        $dispatch_count = 0;

        foreach ($list as $val){
            if ($val['state'] == 0){
                $not_vaild++;
                $dispatch_count++;
            }
            else{
                if ($val['staff_id'] == $val['com_staff_id']){
                    $vaild++;
                    $dispatch_count++;
                }
            }
        }

        return [$vaild,$not_vaild,$dispatch_count];
    }

    //分析
    function analyze($city_id,$condition,$use_phone = 1){
        $condition['not_state'] = 101;

        $list = $this->query_list($city_id,$condition);

        $repark_list =[];
        if ($use_phone == 1) {
            foreach ($list as $val){

                if ($val['state'] == 0){
                    if (!isset($repark_list[$val['staff_phone']])){
                        $repark_list[$val['staff_phone']] = 1;
                    }
                    else
                        $repark_list[$val['staff_phone']]++;
                }
                else{
                    if ($val['staff_id'] == $val['com_staff_id']){
                        if (!isset($repark_list[$val['staff_phone']])){
                            $repark_list[$val['staff_phone']] = 1;
                        }
                        else
                            $repark_list[$val['staff_phone']]++;
                    }
                }
            }
        }
        else if ($use_phone == 2)
        {
            foreach ($list as $val){

                if ($val['state'] == 0){
                    if (!isset($repark_list[$val['staff_id']])){
                        $repark_list[$val['staff_id']] = 1;
                    }
                    else
                        $repark_list[$val['staff_id']]++;
                }
                else{
                    if ($val['staff_id'] == $val['com_staff_id']){
                        if (!isset($repark_list[$val['staff_id']])){
                            $repark_list[$val['staff_id']] = 1;
                        }
                        else
                            $repark_list[$val['staff_id']]++;
                    }
                }
            }
        }


        return $repark_list;
    }
}